package com.iotek.bookstore.web.controlle;

import com.iotek.bookstore.modle.entity.*;
import com.iotek.bookstore.modle.service.serviceimpl.BookBizImpl;
import com.iotek.bookstore.modle.service.serviceimpl.OrderBizImpl;
import com.iotek.bookstore.modle.service.serviceimpl.OrderDetailsBizImpl;
import net.sf.json.JSON;
import net.sf.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/login/createOrderServlet")
public class CreateOrderServlet extends CommonServlet{
    @Override
    public void doInsert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        double totalPrice=0;
        List<Cart>cartList=(List<Cart>)req.getSession().getAttribute("cart");
        for (Cart cart:cartList){
            double price=cart.getPrice()*cart.getNumber();
            totalPrice+=price;
        }
        int userId=(Integer)req.getSession().getAttribute("userId");
        long addressId=(Long)req.getAttribute("addressId");
        long orderId=Long.valueOf(String.valueOf(userId)+System.currentTimeMillis());
        System.out.println("orderId:"+orderId);
        Order order=new Order(orderId,userId,addressId,totalPrice);
        new OrderBizImpl().insert(order);//添加用户订单信息
        for(Cart cart:cartList){
            String book=cart.getBook();
            double price=cart.getPrice();
            int number=cart.getNumber();
            int bookId=cart.getBookId();
            //保存购买书籍的库存信息
            int repertory=0;
            List<Book>books=new BookBizImpl().queryAll();
            for (Book book1:books){
                if ( book1.getBookId()==bookId){
                    repertory=book1.getRepertory();
                }
            }
            repertory=repertory-number;
            Book book1=new Book();
            book1.setBookId(bookId);
            book1.setRepertory(repertory);
            book1.setSales(number);
            new BookBizImpl().update(book1);//更新图书销量和库存
            OrderDetails orderDetails=new OrderDetails(orderId,book,price,number);
            new OrderDetailsBizImpl().insert(orderDetails);//添加订单详情信息
        }
        cartList.clear();//清空购物车
        req.setAttribute("order",order);
        req.getRequestDispatcher("createOrder.jsp").forward(req,resp);
    }

    @Override
    public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    public void doUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    public void doQuery(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String type=req.getParameter("type");
            if(type.equals("user")){
                doQueryUserOrder(req,resp);
            }else if (type.equals("all")){
                doQueryAllOrder(req,resp);
            }
    }

    /*
    * 查询所有用户的订单
    * */
    public void doQueryAllOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        List<Order>orderList=new OrderBizImpl().queryAll();
        JSON json=JSONArray.fromObject(orderList);
        resp.getWriter().write(json.toString());
        System.out.println("CreateOrderServlet----doQueryAllOrder:");
        System.out.println(json.toString());
    }

    /*
    * 根据用户ID查询一个用户的订单信息
    * */
    public void doQueryUserOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        int userId=(Integer)req.getSession().getAttribute("userId");
        List<Order>orderList=new OrderBizImpl().query(userId);
        JSON json=JSONArray.fromObject(orderList);
        resp.getWriter().write(json.toString());
        System.out.println("CreateOrderServlet----doQueryAllOrder:");
        System.out.println(json.toString());
    }
}
